﻿package net.wangit.jdbc.sqltype;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.wangit.jdbc.util.SqlDateUtil;


public class DateSqlType extends NullableSqlType {

	private static final long serialVersionUID = 1L;

	public DateSqlType() {
	}

	public String get(ResultSet rs, String name) throws SQLException {
		String value = rs.getString(name);
		if (value == null)
			return null;
		else
			return value.endsWith(".0") ? value
					.substring(0, value.length() - 2) : value;
	}

	protected void setNotNull(PreparedStatement st, String value, int index)
			throws SQLException {
		if ("".equals(value))
			st.setNull(index, sqlType());
		else
			st.setDate(index, SqlDateUtil.getSqlDate(value));
	}

	public int sqlType() {
		return Types.DATE;
	}
}
